package com.fz.song.shardingspheredemo.conifg.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

public class MyPreciseTableShardingAlgorithm implements PreciseShardingAlgorithm<Long> {


    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        String logicTableName = preciseShardingValue.getLogicTableName();
        Long value = preciseShardingValue.getValue();
        String key = logicTableName + "_" + ((value % 2) + 1);
        if (collection.contains(key)) {
            return key;
        }
        throw new UnsupportedOperationException("分库分表:" + key + "不存在");
    }
}
